//
//  GenericPayFinishInfoController.m
//  HuLaQuan
//
//  Created by liyan on 16/1/22.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "GenericPayFinishInfoController.h"
//cell

#import "GenericSelectedBankCell.h"
#import "GenericSelectedBankTypeCell.h"
#import "ConfirmButtonCell.h"

#import "YuUserCellLabel.h"
@interface GenericPayFinishInfoController ()<UITableViewDataSource,UITableViewDelegate>



@end

@implementation GenericPayFinishInfoController
- (NSArray *)userCardList
{
    if (!_userCardList) {
        _userCardList = [[NSArray alloc] init];
    }
    return _userCardList;
}
-(void)setupUI
{
    [self setupNavBackButton];
   
//    //register

    [self.payTableView registerNib:[UINib nibWithNibName:@"GenericSelectedBankCell" bundle:nil] forCellReuseIdentifier:@"select"];
    [self.payTableView registerNib:[UINib nibWithNibName:@"GenericSelectedBankTypeCell" bundle:nil] forCellReuseIdentifier:@"selectType"];
    [self.payTableView registerNib:[UINib nibWithNibName:@"ConfirmButtonCell" bundle:nil] forCellReuseIdentifier:@"pay"];
    
    
  //  self.title = self.navigationItem.title;
    
    self.payTableView.backgroundColor = UIColorFromRGBA(kBackgroundColor, 1.0);
    
    [self setupChildControllerUI];
}
-(void)setupChildControllerUI
{
    
}

#pragma mark UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return indexPath.section ==1 ? kConfirmButtonCellHeight : kNormalCellHeight;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1) {
        return 1;
    }else{
        return 1 + self.userCardList.count;
    }
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 40;
    }else{
        return 0.000001;
    }
    
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{
    if(section == 0){
        CGRect frameRect = CGRectMake(25, 0, 200, 40);
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
        YuUserCellLabel *textSection = [[YuUserCellLabel alloc] initWithFrame:frameRect];
        textSection.text = @"请选择支付所需要的银行卡";
        [view addSubview:textSection];
        return view;
    }else{
        return nil;
    }
    
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (1 == indexPath.section) {
//        self.payTableView.allowsMultipleSelectionDuringEditing=YES;
//        [self.payTableView setEditing:YES]; //////设置uitableview为编译状态
//    }
    switch (indexPath.section) {

        case 0:
        {
            if (0 == indexPath.row) {
                GenericSelectedBankCell *cell = [tableView dequeueReusableCellWithIdentifier:@"select"];
                cell.bankName.text = @"银联";
               // [cell.selectRound setImage:[UIImage imageNamed:@"tuoyuan"] forState:UIControlStateNormal];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.userInteractionEnabled = NO;
                return cell;
            }
           else
           {
               GenericSelectedBankTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"selectType"];
               
               
               NSString *cardId = self.userCardList[indexPath.row - 1][@"cardId"];

               cell.bankName.text =[NSString stringWithFormat:@"%@ %@ (%@)",self.userCardList[indexPath.row - 1][@"bankName"],[self cardType:self.userCardList[indexPath.row - 1][@"accType"]],[cardId substringFromIndex:(cardId.length - 4)]] ;
               
               cell.selectionStyle = UITableViewCellSelectionStyleNone;
               return cell;
           }
        }
            break;
        default:
        {
            ConfirmButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pay"
                                                                      forIndexPath:indexPath];
            
            [cell.confirmButton setTitle:@"支付" forState:UIControlStateNormal];
            cell.confirm = (ClickBlock)^(){
                [self tapPayButton];
            };
            return cell;
        }
            break;
    }
}
-(void)tapPayButton
{
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


        if (0 ==indexPath.row) {
            
            //设置cell的选中属性为如下就没有点击效果了
            //            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            //            [cell.selectRound setImage:[UIImage imageNamed:@"tuoyuan"] forState:UIControlStateNormal];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }else{
            if (![self.selectedPath isEqual:indexPath]) {
                self.selectedPath = indexPath;
                [self changeCard];
            }
            
        }
        
        [tableView selectRowAtIndexPath:self.selectedPath animated:NO scrollPosition:UITableViewScrollPositionTop];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
